package com.sita.tboard.ui.activity;

import android.os.Bundle;
import android.support.v7.app.ActionBar;
import android.support.v7.widget.Toolbar;
import android.view.MenuItem;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;

import com.sita.bike.R;
import com.sita.bike.rest.RestClient;
import com.sita.bike.rest.model.Rank;
import com.sita.bike.rest.model.RankResponse;
import com.sita.bike.rest.model.response.MileageSummaryRankingResponse;
import com.sita.bike.utils.AccountUtils;
import com.sita.tboard.ActivityBase;
import com.sita.tboard.ui.adapter.RankingAdapter;
import com.sita.tboard.ui.tools.L;

import java.util.ArrayList;
import java.util.List;

import butterknife.Bind;
import butterknife.ButterKnife;
import butterknife.OnClick;
import retrofit.Callback;
import retrofit.RetrofitError;
import retrofit.client.Response;

public class RankingActivity extends ActivityBase {

    @Bind(R.id.rankingListView)
    ListView rankListView;

    @Bind(R.id.ranking)
    TextView rankingView;

    @Bind(R.id.img_rank_back)
    ImageView img_back;

//    @Bind(R.id.toolbar)
//    Toolbar toolbar;

    public List<Rank> list;
    public RankingAdapter rankingadapter;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_ranking);
        ButterKnife.bind(this);

//        setSupportActionBar(toolbar);
//        ActionBar actionBar = getSupportActionBar();
//        actionBar.setDisplayHomeAsUpEnabled(true);
//        actionBar.setDisplayShowTitleEnabled(false);
//        toolbar.setNavigationIcon(R.mipmap.ic_back);

        getMyRanking();

        getRank();
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        switch (item.getItemId()) {
            case android.R.id.home:
                finish();
                return true;
            case R.id.action_settings:
                return true;
        }
        return super.onOptionsItemSelected(item);
    }

    public void getRank() {
        RestClient.getRestNormalService().getRanking(AccountUtils.getAccountID(), new Callback<RankResponse>() {
            @Override
            public void success(RankResponse rankResponse, Response response) {
                if (response.getStatus() == 200) {
                    if (rankResponse.mErrorCode.equals("0") && rankResponse.mData.size() > 0
                            && rankResponse.mData != null) {
                        if (list == null) {
                            list = new ArrayList<Rank>();
                        }

                        if (list.size() > 0) {
                            list.clear();
                        }

                        list.addAll(rankResponse.mData);

                        if (rankingadapter == null) {
                            rankingadapter = new RankingAdapter(RankingActivity.this, list);
                            rankListView.setAdapter(rankingadapter);
                        }
                        rankingadapter.notifyDataSetChanged();
                    }
                }
            }

            @Override
            public void failure(RetrofitError error) {

            }
        });
    }

    public void getMyRanking() {
        AccountUtils.getRank();
        String ranking = getString(R.string.my_ranking_is, AccountUtils.getRank());
        rankingView.setText(ranking);

        RestClient.getRestNormalService().mileageSummaryRanking(AccountUtils.getAccountID(), new Callback<MileageSummaryRankingResponse>() {
            @Override
            public void success(MileageSummaryRankingResponse rankResponse, Response response) {
                if (response.getStatus() == 200) {
                    if (rankResponse.errorCode.equals("0")) {
                        String ranking = getString(R.string.my_ranking_is,
                                rankResponse.data.ranking);
                        rankingView.setText(ranking);
                    }
                }
            }

            @Override
            public void failure(RetrofitError error) {
                L.e(error);
            }
        });
    }

    @OnClick(R.id.img_rank_back)
    public void back() {
        finish();
    }

}
